/* WELTWEITIMNETZ BROWSER - Network Access Manager
 *
 * Copyright (C) Philipp Ruppel, weltweitimnetz.de
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#include "networkreplies.h"
#include <QTimer>
#include <math.h>

void ErrorNetworkReply::abort()
{

}
qint64 ErrorNetworkReply::readData(char *data, qint64 maxSize)
{
    return 0;
}

ErrorNetworkReply::ErrorNetworkReply(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QObject* parent) : QNetworkReply(parent)
{
    setUrl(req.url());

    setOperation(op);



    QTimer::singleShot(500, this, SLOT(errorNetworkReplyReady()));
}

void ErrorNetworkReply::errorNetworkReplyReady()
{
    setError(QNetworkReply::TimeoutError, tr("Conection timed out."));

    emit metaDataChanged();
    emit readyRead();
    emit finished();
}

ArrayNetworkReply::ArrayNetworkReply(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QByteArray data, QObject* parent) : QNetworkReply(parent)
{
    //content = "hahahahaahhahaha";
    //offset = 0;

    buffer.setData(data);


    //setOperation( QNetworkAccessManager::GetOperation );
    setRequest( req );

    setUrl(req.url());

    setOperation(op);

    setError(QNetworkReply::NoError, tr("No Error"));


    //QTimer::singleShot(0, this, SLOT(readyRead()));
    //QTimer::singleShot(0, this, SLOT(finished()));

    open(QIODevice::ReadOnly /*| QIODevice::Unbuffered*/);

    QTimer::singleShot(0, this, SLOT(arrayNetworkReplyReady()));
}
ArrayNetworkReply::~ArrayNetworkReply()
{
    close();
}
void ArrayNetworkReply::close()
{
    QNetworkReply::close();
}

void ArrayNetworkReply::arrayNetworkReplyReady()
{
    setHeader(QNetworkRequest::ContentTypeHeader, "text/html; charset=UTF-8");
    setHeader(QNetworkRequest::ContentLengthHeader, buffer.size());
    setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 200);
    setAttribute(QNetworkRequest::HttpReasonPhraseAttribute, QByteArray("Ok"));
    emit metaDataChanged();
    emit downloadProgress(buffer.size(), buffer.size());
    emit readyRead();
    emit finished();
}

/*bool ArrayNetworkReply::isSequential()
{
    return true;
}*/

qint64 ArrayNetworkReply::bytesAvailable()
{
    /*qint64 avail = content.length() - offset;
    if(avail < 0)
        avail = 0;
    return avail;*/
    return buffer.bytesAvailable();
}

void ArrayNetworkReply::abort()
{
}

/*qint64 ArrayNetworkReply::readData ( char * data, qint64 maxSize )
{
    qint64 off = bytesAvailable();
    if(off > maxSize)
        off = maxSize;
    //char* d = content.data();
    for(qint64 i = 0; i < off; i++)
    {
        data[i] = content.data()[i + offset];
    }
    offset += off;
    return off;
}*/

qint64 ArrayNetworkReply::readData(char *data, qint64 maxSize)
{
    return buffer.read(data, maxSize);
}
